<?php
//function __autoload($classname){require(SITE_DIR."/core/wizard/".$classname.".class.php");}
class CompanyCategoryWizard extends CatalogCategoryWizard
{
	var $db_item;
	var $catalog = "company";
	var $tree = "company_sphere_tree";
	function catalogFullTree($parent = 0, $selected = 0, $main = 0, $status=1, $level_limiter = 0, $level=0)
	{
		//static $selected,$level_limiter;
		$level_limiter = intval($level_limiter);
		$status = intval($status);
		$parent = intval($parent);
		$level = intval($level);
		$pages = array();
		$inside_selected=0;
		/** Query for category non-top level **/
		$query_categories = "
		SELECT 
		c.$this->iname,c.$this->pname,c.title,c.alias,count(ch.id) AS childs 
		FROM $this->table AS c 
		LEFT JOIN $this->table AS ch ON (c.$this->iname=ch.$this->pname) 
		WHERE c.$this->pname = '$parent' AND c.$this->iname<>'0' AND c.display>='$status' 
		GROUP BY c.$this->iname 
		ORDER BY c.order_num";
		$rows = $this->db->fetch_array($query_categories);
		$i = 0;
		foreach ($rows as $k=>$row)
		{
			$query_items = "
			SELECT 
			COUNT(cat.id) AS items_qnty 
			FROM $this->tree AS t,$this->catalog AS cat 
			WHERE t.category_id = '".$row[$this->iname]."' AND cat.id = t.company_id";
			$items_qnty = $this->db_item->fetch_one($query_items);
			$row['firms_qnty'] = $items_qnty[0];
			$pages[$i][$this->iname] = $row[$this->iname];
			if (is_array($selected))
			{
	            if (in_array($row[$this->iname], $selected))
	    		{
	    			$pages[$i]['selected'] = 'selected';
	    			$inside_selected=1;
	    		}
	        }
	        else
	        {
	    		if (intval($selected) == $row[$this->iname])
	    		{
	    			$pages[$i]['selected'] = 'selected';
	    			$inside_selected=1;
	    		}
	    	}
			if($row[$this->iname]==$main) $pages[$i]['main'] = $main;
			$pages[$i]['title'] = $row['title'];
			$pages[$i]['level'] = $level;
			$pages[$i]['alias'] = $row['alias'];
			$pages[$i]['childs']=intval($row['childs']);
			$pages[$i]['firms_qnty']=intval($row['firms_qnty']);
	
			$next_level = $level + 1;
			if($level_limiter==0 or $next_level<=$level_limiter)
			{
				if ($pages[$i]['childs']>0)
				{
		            $sub = $this->recurse($row[$this->iname], $selected,$main, $status, $level_limiter, $next_level);
					$pages[$i]['sub']=$sub['pages'];
					if($sub['inside_selected']==1)
					{
						$pages[$i]['inside_selected']=1;
						$inside_selected=1;
					}
				}
			}
			$i++;
		}
		//print_var($pages);
		return array('pages'=>$pages,'inside_selected'=>$inside_selected);
	}
}
?>